import Common from '../common'
var Js = Common('role')
export default {
	name: 'role',
	components: {},
	mixins: [Js],
	data() {
		return {
			mainData: [],
			self: this,

			table_arguments: {
				height: '70%',
				row_key: 'id',
				tree_props: '',
				default_expand_all: false,
				expand: false,
				selection: true,
				cell_style: {}
			},
			fields: [{
					key: '',
					label: '展开',
					application: [],
					componentName: 'sls-input',
					listType: 'normal',
					width: 80
				},
				{
					key: 'id',
					label: 'ID',
					application: [],
					componentName: 'sls-input',
					listType: 'normal',
					width: 80
				},
				{
					key: 'name',
					label: '角色名称',
					application: ['添加', '编辑'],
					componentName: 'sls-input',
					listType: 'normal',
				},
				{
					key: "status",
					label: '状态',
					application: [],
					componentName: 'sls-select',
					optionsName: 'statusOptions',
					defaultProps: {
						label: 'text',
						value: 'value',
					},
					formatter: function(val) {
						if (val.status == 1) {
							return '启用';
						} else {
							return '关闭';
						};
					}
				},
				{
					key: "type",
					label: '类型',
					application: ['编辑', '添加'],
					componentName: 'sls-select',
					optionsName: 'typeOptions',
					defaultProps: {
						label: 'text',
						value: 'value',
					},
				},
				{
					key: "scope_type",
					label: '权限级别',
					application: ['编辑', '添加'],
					componentName: 'sls-select',
					optionsName: 'scopeOptions',
					defaultProps: {
						label: 'text',
						value: 'value',
					},
				},
				{
					key: 'parentid',
					label: '上级菜单',
					application: ['编辑', '添加'],
					dialogStyle: 'width:100%;',
					customSlot: 'mainImg',
					componentName: 'sls-cascader',
					optionsName: 'labelOptions',
					listType: '',
					limit: 10,
					defaultProps: {
						children: 'children',
						label: 'name',
						value: 'id',
					},
          changeFunc: function(val, self) {
            self.submitData.parentid = val[val.length-1];
          },
				},
				{
					key: 'create_time',
					label: '创建时间',
					listType: 'normal',
					placeholder: '请选择创建时间',
					header_search: {
						componentName: 'sls-date-time-range',
						style: 'width:160px;margin-right:2px;',
						start_placeholder: '创建开始时间',
						end_placeholder: '创建结束时间',
						changeFunc: function(value, self) {
							if (!value) {
								delete self.searchItem.create_time;
							} else {
								self.searchItem.create_time = ['between', value = value.map(function(e) {
									return e / 1000;
								})]
							};

						},
					},
					width: 200,
				},
				{
					label: '操作',
					listType: 'deal',
					width: 200
				},
			],

			// 按钮配置
			btn_info: [

				{
					type: 'info',
					icon: 'edit',
					size: 'mini',
					position: 'list',
					text: function(data) {
						return '编辑';
					},
					func: {
						apiName: function(self) {
							return "api_roleUpdate"
						},
						formData: function(self) {
							return self.formData
						},
						postData: function(self) {
							var postData = {
								searchItem: {
									id: self.originFormData.id,
								},
								data: self.submitData
							}
							return postData;
						}
					},
				},
				{
					type: 'info',
					icon: 'edit',
					size: 'mini',
					position: 'list',
					text: function(data) {
						return '管理权限'
					},
					funcType: 'func',
					func: {
						func: function(self) {
							self.$router.push({
								path: '/home/access',
								name: '权限管理',
								params: {
									role: self.originFormData.id,
									user_no: self.originFormData.user_no,
                  behavior:1,
									type: 'role',
									path: '/home/role',
								}
							});
						},
					},
				},
				{
					type: 'danger',
					icon: 'delete',
					size: 'mini',
					funcType: 'submit',
					position: 'header',
					text: function(data) {
						return '删除选中'
					},
					func: {
						apiName: function(self) {
							return "api_roleUpdate"
						},
						postData: function(self) {
							var deleteArray = [];
							for (var i = 0; i < self.selectionArray.length; i++) {
								deleteArray.push(self.selectionArray[i].id);
							};
							var postData = {
								searchItem: {
									id: ['in', deleteArray],

								},
								data: {
									status: -1
								}
							};
							return postData;
						}
					},
				},
				{
					type: 'info',
					icon: 'edit',
					size: 'mini',
					position: 'header',
					text: function(data) {
						return '添加'
					},
					func: {
						apiName: function(self) {
							return "api_roleAdd"
						},
						formData: function(self) {
							var data = {
								name: '',
                parentid:''
							};
							return data
						},
						postData: function(self) {
							var postData = {
								data: self.submitData
							};
							return postData;
						}
					},
				},
			],

			paginate: {
				count: 0,
				currentPage: 1,
				pagesize: 10,
				is_page: true,
				page_sizes: [10, 30, 60, 90],
				layout: 'total, sizes, prev, pager, next, jumper',
			},
			searchItem: {

			},
			startSearchItem: {

			},
			optionData: {
				labelOptions: [],
				statusOptions: [{
					text: '启用',
					value: 1
				}, {
					text: '禁用',
					value: -1
				}],
				typeOptions: [{
					text: '角色名称',
					value: 0
				}, {
					text: '部门名称',
					value: 1
				}],
				scopeOptions: [{
					text: '只下级',
					value: 0
				}, {
					text: '同级以及下级',
					value: 1
				}],
			},
			otherData: {},
			UserInfo: {
				tableName: 'UserInfo',
				searchItem: {},
				key: 'user_no',
				middleKey: 'user_no',
				condition: 'in',
			},
			dialog: {
				formLabelWidth: 'auto',
				dialogFormVisible: false,
				title: '用户'
			},
			getBefore: {},
			formLabelWidth: 'auto',
			btnName: '',
			formData: {},
			btnNow: {},
			submitData: {},
			originFormData: {},
			selectionArray: [],


		}

	},

	mounted() {
		this.init()
	},
	computed: {
		token: function() {
			return this.$store.getters.getToken
		}
	},
	methods: {

		/**
		 * 初始化
		 */
		init() {
			this.initMainData()
		},

		clearSearch() {
			const self = this;
			self.searchItem = {};
			self.initMainData(true);
		},

		tableRowClassName({
			row,
			rowIndex
		}) {
			console.log(row)
			if (row.children && row.children.length > 0) {
				return 'open-row';
			}
			return '';
		},

		/**
		 * 列表主函数
		 */
		async initMainData(isNew) {

			const self = this;
			const postData = {};
			if (isNew) {
				self.paginate.currentPage = 1;
			};
			//postData.paginate = self.$$cloneForm(self.paginate);
			postData.token = self.$store.getters.getToken;
			if (self.searchItem) {
				postData.searchItem = self.$$cloneForm(self.searchItem)
			};
			if (JSON.stringify(self.getBefore) != "{}") {
				postData.getBefore = self.$$cloneForm(self.getBefore);
			};

			var res = await self.$$api_roleGet({
				data: postData
			});
			self.mainData = res.info.data;
			self.optionData.labelOptions = res.info.data;
			self.paginate.count = res.info.total;

		},


	},


}
